home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 13 / Game / Menu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-29  |  6.3 KB  |  188 lines

  1. //-----------------------------------------------------------------------------
  2. // Menu.h implementation.
  3. // Refer to the Menu.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Main.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // Allows the menu dialog to update the sessions list.
  12. //-----------------------------------------------------------------------------
  13. void UpdateSessionsList( HWND window )
  14. {
  15.     // Enumerate the current sessions on the network.
  16.     g_engine->GetNetwork()->EnumerateSessions();
  17.  
  18.     // Try to keep the same session selected, if it still exists.
  19.     SessionInfo *selectedSession = NULL;
  20.     int selected = (int)SendMessage( GetDlgItem( window, IDC_SESSIONS ), LB_GETCURSEL, 0, 0 );
  21.     if( selected != LB_ERR )
  22.         selectedSession = (SessionInfo*)SendMessage( GetDlgItem( window, IDC_SESSIONS ), LB_GETITEMDATA, selected, 0 );
  23.  
  24.     // Tell the sessions list box not to redraw itself since it will change.
  25.     SendMessage( GetDlgItem( window, IDC_SESSIONS ), WM_SETREDRAW, false, 0 );
  26.  
  27.     // Clear the contents of the sessions list box.
  28.     SendMessage( GetDlgItem( window, IDC_SESSIONS ), LB_RESETCONTENT, 0, 0 );
  29.  
  30.     // Go through the list of sessions found on the local network.
  31.     char name[MAX_PATH];
  32.     SessionInfo *session = g_engine->GetNetwork()->GetNextSession( true );
  33.     while( session != NULL )
  34.     {
  35.         // Convert this session's name into a character string.
  36.         wcstombs( name, session->description.pwszSessionName, MAX_PATH );
  37.  
  38.         // Add this session to the sessions list box.
  39.         int index = (int)SendMessage( GetDlgItem( window, IDC_SESSIONS ), LB_ADDSTRING, 0, (LPARAM)name );
  40.         SendMessage( GetDlgItem( window, IDC_SESSIONS ), LB_SETITEMDATA, index, (LPARAM)session );
  41.  
  42.         // Check if this is the session the was selected before.
  43.         if( selectedSession == session )
  44.             SendMessage( GetDlgItem( window, IDC_SESSIONS ), LB_SETCURSEL, index, 0 );
  45.  
  46.         // Go to the next session.
  47.         session = g_engine->GetNetwork()->GetNextSession();
  48.     }
  49.  
  50.     // If there was no selected session, then select the first session.
  51.     if( selectedSession == NULL )
  52.         SendMessage( GetDlgItem( window, IDC_SESSIONS ), LB_SETCURSEL, 0, 0 );
  53.  
  54.     // Tell the sessions list box to redraw itself now.
  55.     SendMessage( GetDlgItem( window, IDC_SESSIONS ), WM_SETREDRAW, true, 0 );
  56.     InvalidateRect( GetDlgItem( window, IDC_SESSIONS ), NULL, false );
  57. }
  58.  
  59. //-----------------------------------------------------------------------------
  60. // Call back function for the menu's dialog.
  61. //-----------------------------------------------------------------------------
  62. int CALLBACK MenuDialogProc( HWND window, UINT msg, WPARAM wparam, LPARAM lparam )
  63. {
  64.     // These are used to keep the text in the edit boxes between state changes.
  65.     static char name[MAX_PATH] = "Unknown Player";
  66.     static char character[MAX_PATH] = "Marine.txt";
  67.     static char map[MAX_PATH] = "Abandoned City.txt";
  68.  
  69.     switch( msg )
  70.     {
  71.         case WM_INITDIALOG:
  72.         {
  73.             // Show the player's name, selected character, and selected map.
  74.             SetWindowText( GetDlgItem( window, IDC_NAME ), name );
  75.             SetWindowText( GetDlgItem( window, IDC_CHARACTER ), character );
  76.             SetWindowText( GetDlgItem( window, IDC_MAP ), map );
  77.  
  78.             // Allow the sessions list to update.
  79.             UpdateSessionsList( window );
  80.  
  81.             return true;
  82.         }
  83.  
  84.         case WM_COMMAND:
  85.         {
  86.             switch( LOWORD( wparam ) )
  87.             {
  88.                 case IDC_HOST:
  89.                 {
  90.                     // Get the character and map.
  91.                     PlayerData data;
  92.                     GetWindowText( GetDlgItem( window, IDC_CHARACTER ), character, MAX_PATH );
  93.                     GetWindowText( GetDlgItem( window, IDC_MAP ), map, MAX_PATH );
  94.                     strcpy( data.character, character );
  95.                     strcpy( data.map, map );
  96.  
  97.                     // Get the player's name.
  98.                     GetWindowText( GetDlgItem( window, IDC_NAME ), name, MAX_PATH );
  99.  
  100.                     // Create a session name using the player's name.
  101.                     char session[MAX_PATH];
  102.                     sprintf( session, "%s's Session", name );
  103.  
  104.                     // Host a new session then switch to the game state.
  105.                     if( g_engine->GetNetwork()->Host( name, session, 8, &data, sizeof( data ) ) )
  106.                     {
  107.                         g_engine->ChangeState( STATE_GAME );
  108.  
  109.                         EndDialog( window, true );
  110.                     }
  111.  
  112.                     return true;
  113.                 }
  114.  
  115.                 case IDC_SESSIONS:
  116.                 {
  117.                     // Check if the user double clicked on the session list.
  118.                     if( HIWORD( wparam ) != LBN_DBLCLK )
  119.                         return true;
  120.                 } // If so, then fall through to IDC_JOIN.
  121.  
  122.                 case IDC_JOIN:
  123.                 {
  124.                     // Get the character.
  125.                     PlayerData data;
  126.                     GetWindowText( GetDlgItem( window, IDC_CHARACTER ), character, MAX_PATH );
  127.                     strcpy( data.character, character );
  128.  
  129.                     // Get the player's name.
  130.                     GetWindowText( GetDlgItem( window, IDC_NAME ), name, MAX_PATH );
  131.  
  132.                     // Get the selected session.
  133.                     int session = (int)SendMessage( GetDlgItem( window, IDC_SESSIONS ), LB_GETCURSEL, 0, 0 );
  134.  
  135.                     // Join the selected session then switch to the game state.
  136.                     if( g_engine->GetNetwork()->Join( name, session, &data, sizeof( data ) ) )
  137.                     {
  138.                         g_engine->ChangeState( STATE_GAME );
  139.  
  140.                         EndDialog( window, true );
  141.                     }
  142.                     else
  143.                     {
  144.                         // If the join failed, it may be because the session
  145.                         // doesn't exist any more, so refresh the session list.
  146.                         UpdateSessionsList( window );
  147.                     }
  148.  
  149.                     return true;
  150.                 }
  151.  
  152.                 case IDC_REFRESH:
  153.                 {
  154.                     // Refresh the session list.
  155.                     UpdateSessionsList( window );
  156.  
  157.                     return true;
  158.                 }
  159.  
  160.                 case IDC_EXIT:
  161.                 {
  162.                     PostQuitMessage( 0 );
  163.  
  164.                     return true;
  165.                 }
  166.             }
  167.         }
  168.     }
  169.  
  170.     return false;
  171. }
  172.  
  173. //-----------------------------------------------------------------------------
  174. // Menu class constructor.
  175. //-----------------------------------------------------------------------------
  176. Menu::Menu() : State( STATE_MENU )
  177. {
  178.     // Does nothing but set's the state's ID.
  179. }
  180.  
  181. //-----------------------------------------------------------------------------
  182. // Update the menu state.
  183. //-----------------------------------------------------------------------------
  184. void Menu::Update( float elapsed )
  185. {
  186.     // Display the menu dialog. Processing will hang here until this is closed.
  187.     DialogBox( NULL, MAKEINTRESOURCE( IDD_MENU ), g_engine->GetWindow(), MenuDialogProc );
  188. }